<?php
// $Id$

/**
 * Provides the cv2 content type.
 * @file
 */

/**
 * implementation of hook_help.
 */
function cv2_help($path, $arg) {
  
  if ($path == 'admin/help#cv2') {
    $txt = 'A cv2 is a textual description of a '
      .'person\'s life and works. The summary should give '
      .'a brief overview of the person explaining why the '
      .'person is important. The \'dates\' area should '
      .'provide information on birth date and death date for '
      .'the person. This field is free-text, and might look '
      .'like this: \'c. 500 BCE-450 CE\'. \'Life\' should '
      .'provide a cv2 of the person, and \'Works\' '
      .'should contain a list of this person\'s works.';
    $replace = array();
    return '<p>'. t($txt, $replace) .'</p>';
  }
}

/**
 * Implements hook_node_info()
 */
function cv2_node_info() {
  return array(
    'cv2' => array(
      'module' => 'cv2',
      'name' => t('cv2'),
      'description' => t("A cv2 of a person."),
      'has_title' => true,
      'title_label' => t('cv2 of'),
      'has_body' => true,
      'body_label' => t('Overview'),
  	  //'module' => 'cv2',
    )
  );
}

/**
 * Implements hook_perm()
 */
function cv2_perm() {
  return array(
    'create cv2 node',
    'edit cv2 nodes',
    'delete cv2 nodes',
  );
}

/**
 * Implements hook_access()
 */
function cv2_access($op, $node, $account) {
  switch ($op) {
  case 'create':
    return user_access('create cv2 node', $account);
  case 'update':
    return user_access('edit cv2 nodes', $account);
  case 'delete':
    return user_access('delete cv2 nodes', $account);
  }
}

// ======================================
// Database:
// ======================================

/**
 * implements hook_insert()
 */
function cv2_insert($node) {
  
  if (!isset($node->life)) $node->life = '';
  if (!isset($node->works)) $node->life = '';
  
  db_query(
    'INSERT INTO {cv2} (vid, nid, dates, life, works) '
      ."VALUES (%d, %d, '%s', '%s','%s')", 
    $node->vid, 
    $node->nid, 
    $node->dates, 
    $node->life,
    $node->works
  );
}
/**
 * implements hook_update()
 */
function cv2_update($node) {
  if ($node->revision) { // Node is new or a revision. See node_example.module.
    cv2_insert($node);
  } 
  else {
    db_query("UPDATE {cv2} SET dates = '%s', life = '%s', works='%s' "
        ."WHERE vid = %d", 
      $node->dates, 
      $node->life, 
      $node->works,
      $node->vid
    );
  }
}
/**
 * implements hook_delete()
 */
function cv2_delete($node) {
  db_query('DELETE * FROM {cv2} WHERE nid = %d', $node->nid);
}

/**
 * Implements hook_nodeapi()
 * This handles deleting revisions.
 */
function cv2_nodeapi(&$node, $op, $teaser, $page) {
  if ($op == 'delete revision')
      db_query('DELETE * FROM {cv2} WHERE vid = %d', $node->vid);
}

// ======================================
// Creating Data:
// ======================================
/**
 * Implementation of hook_form()
 */
function cv2_form(&$node) {
  $type = node_get_types('type', $node);

  // Existing files: title (cv2 of) and body (Overview)
  if ($type->has_title) {
    $form['title'] = array(
      '#type' => 'textfield',
      '#title' => check_plain($type->title_label),
      '#required' => true,
      '#default_value' => $node->title,
      '#weight' => -5
    );
  }

  if ($type->has_body) {
    $form['body_field'] = node_body_field(
        $node, 
        $type->body_label, 
        $type->min_word_count
      );
  }

  // Our custom fields: Dates, Life, Works.
  // (See FAPI docs for specification)
  $form['dates'] = array(
    '#type' => 'textfield',
    '#size' => 50,
    '#maxlengh' => 127,
    '#title' => t('Dates'),
    '#description' => t("Birth and death dates."),
    '#default_value' => isset($node->dates) ? $node->dates : '',
  );
  $form['life'] = array(
    '#type' => 'textarea',
    '#title' => t('Life'),
    '#cols' => 50,
    '#rows' => 5,
    '#description' => t("A description of this person's life."),
    '#default_value' => isset($node->life) ? $node->life : '',
  );
  $form['works'] = array(
    '#type' => 'textarea',
    '#title' => t('Works'),
    '#cols' => 50,
    '#rows' => 5,
    '#description' => t("An annotated bibliography of this person's works."),
    '#default_value' => isset($node->works) ? $node->works : '',
  );

  return $form;
}

// ======================================
// Getting Data:
// ======================================
/**
 * Implementation of hook_load()
 */
function cv2_load($node) {
  $result = db_query(
    'SELECT dates, life, works FROM {cv2} WHERE vid = %d',
    $node->vid
  );
  return db_fetch_object($result);
}
/**
 * Implementation of hook_view()
 */
function cv2_view($node, $teaser=false, $page=false) {
  $node = node_prepare($node, $teaser); // get it ready for display
  
  $dates = check_plain($node->dates);
  $life = check_markup($node->life);
  $works = check_markup($node->works);
  
  // Add theme stuff here
  $node->content['cv2_info'] = array(
    '#value' => theme('cv2_info', $dates, $life, $works),
    '#weight' => 1,
  );
  
  return $node;
}

// ======================================
// Theme:
// ======================================
/**
 * implements hook_theme()
 */
function cv2_theme() {
  return array(
    'cv2_info' => array(
      'template' => 'cv2_info',
      'arguments' => array(
        'dates' => null,
        'life' => null,
        'works' => null,
      ),
    ),
  );
}